package cn.vivi.recyclercomp.custom;

import java.util.List;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.vivi.recyclercomp.adapter.RecyclerViewBaseAdapter;

import cn.vivi.recyclercomp.R;

public class CustomRecyclerAdapter extends RecyclerViewBaseAdapter<String> {

	private int mBackground;


	public CustomRecyclerAdapter(Context context, List<String> data) {
		super(context, data);
		TypedValue val = new TypedValue();
		this.context.getTheme().resolveAttribute(R.attr.selectableItemBackground, val, true);
		mBackground = val.resourceId;
	}
 

	@Override
	public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ItemViewHolder itemHolder = new ItemViewHolder(inflater.inflate(R.layout.listitem_recycler_item,
				null));
		itemHolder.textView.setMinimumHeight(128);
		itemHolder.textView.setFocusable(true);
		itemHolder.textView.setBackgroundResource(mBackground); 
		RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
		itemHolder.llytItem.setLayoutParams(lp);
		
		return itemHolder;
	}


	@Override
	public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
		ItemViewHolder myHolder = ((ItemViewHolder) holder);
		String itemText = dataList.get(position);
		myHolder.textView.setText(itemText);
		boolean selected = false;
		myHolder.checkBox.setChecked(selected);
		myHolder.textView.setVisibility(View.VISIBLE);
	}

	static class ItemViewHolder extends RecyclerView.ViewHolder {

		public TextView textView;
		public CheckBox checkBox;
		public LinearLayout llytItem;


		public ItemViewHolder(View v) {
			super(v);
			textView = (TextView) v.findViewById(R.id.text);
			checkBox = (CheckBox) v.findViewById(R.id.selected);
			llytItem = (LinearLayout) v.findViewById(R.id.llytItem);
		}


		@Override
		public String toString() {
			return super.toString() + " \"" + textView.getText() + "\"";
		}
	}

}
